<?php
    include_once('../../src/classes/class.login.php');
    include_once('../../src/classes/class.mssql.php');
    
    session_start();
    
    $login = new Login();
    if ($login->giveId() != $_GET['id'])
    {
     header('location: http://'.$_SERVER['HTTP_HOST'].'/');
    }
    
    $db = new MSSQL();
    
    $result = $db->fetch_array($db->query("SELECT comtec_id_mainteiner FROM users WHERE comtec_id_user = ".$login->giveId()));   
    $comtec_id_mainteiner = $result['comtec_id_mainteiner'];
        
    $result = $db->fetch_array($db->query("SELECT comtec_id_owner FROM owner WHERE comtec_id_user = ".$login->giveId()));   
    $comtec_id_owner = $result['comtec_id_owner'];
    
    $result = $db->fetch_array($db->query("SELECT company_name FROM mainteiner WHERE comtec_id_mainteiner = $comtec_id_mainteiner"));   
    $company_name = $result['company_name'];
    
     //machines
    $stmt = $db->query("SELECT count(*) as cant FROM [dbo].[machines] WHERE status_reg=1 AND comtec_id_owner=$comtec_id_owner AND comtec_id_mainteiner = $comtec_id_mainteiner ");
    sqlsrv_fetch($stmt);
    $machines=sqlsrv_get_field($stmt,0);
    
    // N° operaciones
    $stmt = $db->query("SELECT count(*) as c FROM ([dbo].[operation] as o INNER JOIN [dbo].[responsible] as r ON o.comtec_id_responsible = r.comtec_id_responsible) INNER JOIN machines as m ON o.comtec_id_machine = m.comtec_id_machine WHERE m.comtec_id_owner=$comtec_id_owner AND o.status_reg=1 AND r.comtec_id_mainteiner =".$comtec_id_mainteiner);
    sqlsrv_fetch($stmt);
    $operations=sqlsrv_get_field($stmt,0);
    
    // N° operaciones - mantenciones
    $stmt = $db->query("SELECT count(*) as c FROM ([dbo].[operation] as o INNER JOIN [dbo].[responsible] as r ON o.comtec_id_responsible = r.comtec_id_responsible) INNER JOIN machines as m ON o.comtec_id_machine = m.comtec_id_machine WHERE m.comtec_id_owner=$comtec_id_owner AND o.status_reg=1 AND operation_type=1 AND r.comtec_id_mainteiner =".$comtec_id_mainteiner);
    sqlsrv_fetch($stmt);
    $operations_m=sqlsrv_get_field($stmt,0);
    
    // N° operaciones - Emergencias
    $stmt = $db->query("SELECT count(*) as c FROM ([dbo].[operation] as o INNER JOIN [dbo].[responsible] as r ON o.comtec_id_responsible = r.comtec_id_responsible) INNER JOIN machines as m ON o.comtec_id_machine = m.comtec_id_machine WHERE m.comtec_id_owner=$comtec_id_owner AND o.status_reg=1 AND operation_type=2 AND r.comtec_id_mainteiner =".$comtec_id_mainteiner);
    sqlsrv_fetch($stmt);
    $operations_u=sqlsrv_get_field($stmt,0);
    
    // N° operaciones - instalaciones
    $stmt = $db->query("SELECT count(*) as c FROM ([dbo].[operation] as o INNER JOIN [dbo].[responsible] as r ON o.comtec_id_responsible = r.comtec_id_responsible) INNER JOIN machines as m ON o.comtec_id_machine = m.comtec_id_machine  WHERE m.comtec_id_owner=$comtec_id_owner AND o.status_reg=1 AND operation_type=3 AND r.comtec_id_mainteiner =".$comtec_id_mainteiner);
    sqlsrv_fetch($stmt);
    $operations_i=sqlsrv_get_field($stmt,0);
    
    // N° operaciones - reparaciones
    $stmt = $db->query("SELECT count(*) as c FROM ([dbo].[operation] as o INNER JOIN [dbo].[responsible] as r ON o.comtec_id_responsible = r.comtec_id_responsible) INNER JOIN machines as m ON o.comtec_id_machine = m.comtec_id_machine  WHERE m.comtec_id_owner=$comtec_id_owner AND o.status_reg=1 AND operation_type=4 AND r.comtec_id_mainteiner =".$comtec_id_mainteiner);
    sqlsrv_fetch($stmt);
    $operations_r=sqlsrv_get_field($stmt,0);
    
    $token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
    
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>RF Mantencion - Panel</title>
    <link rel="stylesheet" href="../../css/comtec.css" type="text/css">
    <script type="text/javascript" src="../../js/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){   
	 $("#userPanel").load("http://<?=$_SERVER['HTTP_HOST'];?>/div/userPanel.php?id=<?=$_GET['id'];?>");
	 var mainMenu = '<ul class="mainMenu">';
         mainMenu += '<li><a href="http://<?=$_SERVER['HTTP_HOST'];?>/users/clients/panel.php?id=<?=$_GET['id'];?>" style="color: yellow" id="home">Inicio</a></li>';
         mainMenu += '<li><a href="http://<?=$_SERVER['HTTP_HOST'];?>/operations/index.php?id=<?=$_GET['id'];?>">Operaciones</a></li>';        
         mainMenu += '<li><a href="http://<?=$_SERVER['HTTP_HOST'];?>/reports/index.php?id=<?=$_GET['id'];?>">Informes</a></li>';
         mainMenu += '</ul>';
         $("#mainMenu").html(mainMenu);
         $("#machines_list").load("http://<?=$_SERVER['HTTP_HOST'];?>/src/loadMachines.php?id=<?=$_GET['id'];?>");
         $("#operations_list").load("http://<?=$_SERVER['HTTP_HOST'];?>/src/loadOperations.php?id=<?=$_GET['id'];?>");
        });
        function machine(comtec_id_machine){
         $("#subtitle").remove(); 
          $.get("../../src/machine.php", 
          {id: comtec_id_machine},
          function(data){
           $("#infoPanel").html(data);
          }
         ); 
        }
        function loadHome(){
         $.get("panel.php", 
          {id: <?=$_SESSION['comtec_id_user'];?>},
          function(data){
           $("#contenedor").html(data);
          }
         );    
        }
        function loadOperations(comtec_id_machine){
          $("#subtitle").remove(); 
          $.get("../../src/operations.php", 
          {id_machine: comtec_id_machine},
          function(data){
           $("#infoPanel").html(data);
          }
         );    
        }
        function operation(comtec_id_operation,comtec_id_machine){
          $("#subtitle").remove(); 
          $.get("../../src/operation.php", 
          {comtec_id_machine: comtec_id_machine,comtec_id_operation: comtec_id_operation,token: $("#token").val()},
          function(data){
           $("#infoPanel").html(data);
          }
         );    
        }
         function loadOperation(comtec_id_operation,comtec_id_machine){
          $("#subtitle").remove(); 
          $.get("../../src/loadOperation.php", 
           {comtec_id_machine: comtec_id_machine,comtec_id_operation: comtec_id_operation,token: $("#token").val(),
            id_mainteiner: $("#comtec_id_mainteiner").val()},
           function(data){
            $("#infoPanel").html(data);
           }
          );        
         }
         function editOperation(comtec_id_operation){
             
          var errores = '';
          
          var operation_type = $("#operation_type").val();
          if (operation_type == 0)
          {
           errores += '<p class="error">No ha ingresado: <b>tipo de operacion</b><p>';       
          }
          var comtec_id_responsible = $("#comtec_id_responsible").val();
          if (comtec_id_responsible == 0)
          {
           errores += '<p class="error">No ha ingresado: <b>encargado</b><p>';       
          }
          var diagnosis = $("#diagnosis").val();
          var changed_parts = $("#changed_parts").val();
          var notes = $("#notes").val();
          
          if (operation_type == 1){
           var next_day = $("#next_day").val();
           if (next_day == "Dia")
           {
            errores += '<p class="error">No ha ingresado: <b>dia de la fecha prox. mantenci&oacute;n</b><p>';       
           }
           var next_month = $("#next_month").val();
           if (next_month == "Mes")
           {
            errores += '<p class="error">No ha ingresado: <b>mes de la fecha prox. mantenci&oacute;n</b><p>';       
           }
           var next_year = $("#next_year").val(); 
           if (next_year == "Año")//?
           {
            errores += '<p class="error">No ha ingresado: <b>a&ntilde;o de la fecha prox. mantenci&oacute;n</b><p>';       
           }
          }
          
          var value_work = $("#value_work").val();
          if (!((/^([0-9])*$/).test(value_work)))
          {
           errores += '<p class="error">Ha ingresado un dato invalido en el <b>valor mano de obra</b><p>';
          }
          var value_parts = $("#value_parts").val();     
          if (!((/^([0-9])*$/).test(value_parts)))
          {
           errores += '<p class="error">Ha ingresado un dato invalido en el <b>valor de los repuestos</b><p>';
          }
          var value_others = $("#value_others").val();
          if (!((/^([0-9])*$/).test(value_others)))
          {
           errores += '<p class="error">Ha ingresado un dato invalido en <b>otros valores</b><p>';
          }
       
          var op_day1 = $("#op_day1").val();
          if (op_day1 == "00")
          {
           errores += '<p class="error">No ha ingresado: <b>dia de la fecha de inicio</b><p>';       
          }
          var op_month1 = $("#op_month1").val();
          if (op_month1 == "00")
          {
           errores += '<p class="error">No ha ingresado: <b>mes de la fecha de inicio</b><p>';  
          }
          var op_year1 = $("#op_year1").val();
          if (op_year1 == "Año")//?
          {
           errores += '<p class="error">No ha ingresado: <b>a&ntilde;o de la fecha de inicio</b><p>';       
          }
          var hour1 = $("#hour1").val();
          if (hour1 == "Horas")
          {
           errores += '<p class="error">No ha ingresado: <b>hora de inicio (horas)</b><p>';       
          }
          var minute1 = $("#minute1").val();
          if (minute1 == "Minutos")
          {
           errores += '<p class="error">No ha ingresado: <b>hora de inicio (minutos)</b><p>';       
          }
          
          var op_day2 = $("#op_day2").val();
          if (op_day2 == "00")
          {
           errores += '<p class="error">No ha ingresado: <b>dia de la fecha de fin</b><p>';       
          }
          var op_month2 = $("#op_month2").val();
          if (op_month2 == "00")
          {
           errores += '<p class="error">No ha ingresado: <b>mes de la fecha de fin</b><p>';  
          }
          var op_year2 = $("#op_year2").val();
          if (op_year2 == "Año")//?
          {
           errores += '<p class="error">No ha ingresado: <b>a&ntilde;o de la fecha de fin</b><p>';       
          }
          var hour2 = $("#hour2").val();
          if (hour2 == "Horas")
          {
           errores += '<p class="error">No ha ingresado: <b>hora de fin (horas)</b><p>';       
          }
          var minute2 = $("#minute2").val();
          if (minute2 == "Minutos")
          {
           errores += '<p class="error">No ha ingresado: <b>hora de fin (minutos)</b><p>';       
          }
          
          var delay = getDelay();
          var revised = $("#revised").val();     
          var notes_for_owner = $("#notes_for_owner").val();
          if(errores == ''){
         
          $.post("../../src/scripts/editOperation.php",
          {
            editOperation: 2,comtec_id_operation: comtec_id_operation,revised: revised,
            token: $("#token").val(),comtec_id_machine: $("#comtec_id_machine").val(),
            operation_type: operation_type,comtec_id_responsible: comtec_id_responsible,
            op_day1: op_day1,op_month1: op_month1,hour1: hour1,minute1: minute1,
            op_year1:  op_year1,diagnosis: diagnosis,op_day2: op_day2,op_month2: op_month2,
            changed_parts: changed_parts,op_year2: op_year2,hour2: hour2,minute2: minute2,
            notes: notes,value_work: value_work,value_parts: value_parts,delay: delay,
            value_others: value_others,notes_for_owner: notes_for_owner,
            next_day: next_day,next_month: next_month,next_year: next_year 
           },
           function(data){ 
            $("#result").html(data);}
          );
          }else{
                $("#result").html(errores);
               }     
         }
         function getDelay(){
          var delay = 0;   
          var year = $("#op_year1").val();
          var month = $("#op_month1").val();
          var day = $("#op_day1").val();
          var hours = $("#hour1").val();
          var minutes = $("#minute1").val();
          var inicio = new Date(year, month-1, day, hours, minutes, 0, 0);
          year = $("#op_year2").val();
          month = $("#op_month2").val();
          day = $("#op_day2").val();
          hours = $("#hour2").val();
          minutes = $("#minute2").val();
          var fin = new Date(year, month-1, day, hours, minutes, 0, 0);
          if (!isNaN(inicio.getTime()) && !isNaN(fin.getTime())){
           var one_minute=1000*60;
           delay = (fin.getTime()-inicio.getTime())/one_minute;
          }
          return delay;
         }
         function setDelay(){
          var year = $("#op_year1").val();
          var month = $("#op_month1").val();
          var day = $("#op_day1").val();
          var hours = $("#hour1").val();
          var minutes = $("#minute1").val();
          var inicio = new Date(year, month-1, day, hours, minutes, 0, 0);
          year = $("#op_year2").val();
          month = $("#op_month2").val();
          day = $("#op_day2").val();
          hours = $("#hour2").val();
          minutes = $("#minute2").val();
          var fin = new Date(year, month-1, day, hours, minutes, 0, 0);
          if (!isNaN(inicio.getTime()) && !isNaN(fin.getTime())){
           var one_minute=1000*60;
           var delay = (fin.getTime()-inicio.getTime())/one_minute;
           $("#delay").val(delay);
          }          
         }
         
         function operation_type(){
          if($("#operation_type").val()!=1){
            $("#next_date").hide();
          }else{
            $("#next_date").show();
          }           
         }
         
         function addValue(){
          var v1 = parseInt($("#value_work").val());
          var v2 = parseInt($("#value_parts").val());
          var v3 = parseInt($("#value_others").val());
          var v = parseInt(v1+v2+v3);
          if ((/^([0-9])*$/).test(v)){
           $("#value_total").val(v);
          } 
         }
         
    </script>
</head>
<body>
    <div id="contenedor">
        <div id="title"><h1>RF Mantenci&oacute;n</h1><div id="enterpriseData"><h3><?=$company_name?></h3></div></div>
	<div id="userPanel"></div>
	<div id="mainMenu"></div>
        <div id="subtitle"><h2>Inicio</h2></div>
        <div id="infoPanel">
        <table>
            <tr><td></td></tr>
            <tr>
                <td><b>Informaci&oacute;n General</b></td>
            </tr>
            <tr>
                <td>Numero de operaciones: </td><td><b><?=$operations;?></b></td>
            </tr>
            <tr>
                <td></td><td>Mantenciones</td><td><b><?=$operations_m;?></b></td><td><?if($operations>0){echo number_format((($operations_m/$operations)*100), 2, '.', '');}else{echo "0";}?>%</td>
            </tr>
            <tr>
                <td></td><td>Instalaciones</td><td><b><?=$operations_i;?></b></td><td><?if($operations>0){echo number_format((($operations_i/$operations)*100), 2, '.', '');}else{echo "0";}?>%</td>
            </tr>
            <tr>               
                <td></td><td>Emergencias</td><td><b><?=$operations_u;?></b></td><td><?if($operations>0){echo number_format((($operations_u/$operations)*100), 2, '.', '');}else{echo "0";}?>%</td>
            </tr> 
            <tr>               
                <td></td><td>Reparaciones</td><td><b><?=$operations_r;?></b></td><td><?if($operations>0){echo number_format((($operations_r/$operations)*100), 2, '.', '');}else{echo "0";}?>%</td>
            </tr>
            <tr>
                <td>Numero de m&aacute;quinas: </td><td><b><?=$machines;?></b></td>
            </tr>
            <tr>
                <td></td>
            </tr>               
            </table>
            <table>
                <tr><td></td></tr>
            <tr>
                <td colspan="7"><b>Maquinas que se van a operar dentro de hoy y los pr&oacute;ximos 30 d&iacute;as</b></td>    
            </tr>        
            </table>
            <div id="machines_list"></div>
            <table>
                <tr><td></td></tr>
             <tr>
                <td colspan="7"><b>Operaciones realizadas dentro de hoy y los 30 d&iacute;as anteriores</b></td>    
             </tr>
            </table>
            <div id="operations_list"></div>   
         </div>
         <div id="resultPanel"></div>
         <br />
         <input type="hidden" id="comtec_id_mainteiner" value="<?=$comtec_id_mainteiner;?>" />
         <input type="hidden" id="token" value="<?=$token;?>" />
         <div id="footer">&copy; 2011 ComTec Ltda. Todos los derechos reservados.</div>
       </div> 
   </body>
</html>
